package com.garen.fire.biz.impl;

import com.garen.fire.dao.IFireCarDao;
import com.garen.fire.dao.IFireDutyDao;
import com.garen.fire.entity.FireCar;
import com.garen.fire.entity.FireDuty;
import com.garen.sys.entity.FireUser;
import com.garen.fire.biz.IFireDutyBiz;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.garen.common.*;


@Service
public class FireDutyBizImpl implements IFireDutyBiz {

	@Autowired
	private IFireDutyDao fireDutyDao;

	@Autowired
	private IFireCarDao fireCarDao;
	
	/*
	*批量保存
	*/
	public int saveFireDutyAll(MsgBean mbean,Date date,Integer orgId,List<FireDuty> fireDutyList){
		//清空旧的
		FireDuty fireDuty = new FireDuty();
		fireDuty.setFireOrg(orgId);
		fireDuty.setFireDate(date);
		fireDutyDao.del(fireDuty);
		for(FireDuty duty : fireDutyList){
			duty.setFireOrg(orgId);
			duty.setFireDate(date);
			fireDutyDao.add(duty);
		}
		return 1;
	}
	
	// 根据排班记录生成用户
	private FireUser createUserByDuty1(FireDuty duty) {
		FireUser user = new FireUser();
		user.setUserId(duty.getFireMan());
		user.setUserName(duty.getFireManName());
		user.setUserPoliceRank(duty.getFirePoliceRank());
		user.setFireAge(duty.getFireManAge());
		user.setDutyId(duty.getDutyId());
		return user;
	}
		
	// 根据排班记录生成用户
	private JSONObject createUserByDuty(FireDuty duty) {
		JSONObject user = new JSONObject();
		user.put("fireMan", duty.getFireMan());
		user.put("fireManName", duty.getFireManName());
		user.put("userPoliceRank", duty.getFirePoliceRank());
		user.put("fireAge", duty.getFireManAge());
		user.put("dutyId", duty.getDutyId());
		return user;
	}
		
	/*
	* 获取中队排班信息
	*/
	@Override
	public JSONObject getFireDutyList(String orgId,Date kssj,Date jssj){
		 //排班列表
		 //左连接消防员
		 List<FireDuty> fireDutylist =  fireDutyDao.getFireDutyList(orgId,kssj,jssj);
		 JSONArray menList = new JSONArray();
		 //获取所有车辆
		 List<FireCar> carList = fireCarDao.getFireCarList(orgId);
		 //过滤,获取待命消防员
		 for(FireDuty duty : fireDutylist){
			 if(duty.getDutyId() == null){
				 menList.add(createUserByDuty(duty));
			 }
		 }
		 //关联车和人
		 for(FireCar car : carList){
			 JSONArray userList = new JSONArray();
			 for(FireDuty duty : fireDutylist){
				 if(car.getCarId().equals(duty.getFireCar())){
					 userList.add(createUserByDuty(duty));
				 }
			 }
			 car.setMenList(userList);
		 }
		 JSONObject resultMap = new JSONObject();
		 resultMap.put("carList", carList);
		 resultMap .put("userList", menList);
		 return resultMap;
	}
	
	/*
	*分页查询
	*/
	@Override
	public List<FireDuty> getFireDutyList(PageBean<FireDuty> pb,FireDuty fireDuty){
		return fireDutyDao.getPageList(pb,fireDuty);
	}

	/*
	*分页查询
	*/
	@Override
	public List<FireDuty> getFireDutyList(FireDuty fireDuty){
		return fireDutyDao.getList(fireDuty);
	}

	/*
	*查询个体
	*/
	@Override
	public FireDuty getFireDuty(FireDuty fireDuty){
		return fireDutyDao.get(fireDuty);
	}

	/*
	*保存
	*/
	@Override
	public int saveFireDuty(MsgBean mbean,FireDuty fireDuty){
		int ret = -1;
		if(fireDuty.getDutyId() == null){
			ret = fireDutyDao.add(fireDuty);
		}else{
			ret = fireDutyDao.update(fireDuty);
		}
		return ret;
	}

	/*
	*删除
	*/
	@Override
	public int delFireDuty(MsgBean mbean,FireDuty fireDuty){
		return fireDutyDao.del(fireDuty);
	}

}
